package com.xm.rsi.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.xm.rsi.common.ErrorCode;
import com.xm.rsi.exception.BusinessException;

import java.io.InputStream;

public class OssClient {
    
    // 阿里云OSS的访问密钥
    private static final String accessKeyId = "LTAI5t755MwjmawNT71qmLX5";
    private static final String accessKeySecret = "uyQEiyUE9PwEY6hE0p3xM4lxrknFWd";
    
    // OSS存储空间名称
    private static final String bucketName = "rs-cloud-data";
    // 存储路径前缀，可根据需要自定义
    private static final String objectPrefix = "rsi_data/";
    
    public static final String endPoint = "oss-cn-shanghai.aliyuncs.com";

    public static String uploadFileToOSS(InputStream fileInputStream, String fileName) {
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
        String objectKey = objectPrefix + fileName;
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(getContentType(fileName.substring(fileName.lastIndexOf("."))));
        objectMetadata.setContentDisposition("inline");
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectKey, fileInputStream, objectMetadata);
        ossClient.putObject(putObjectRequest);
        return "https://" + bucketName + "." + endPoint + "/" + objectKey;
    }
    
    public static void delFile(String fileName) {
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
        try {
            String objectKey = fileName.split("oss-cn-shanghai.aliyuncs.com/")[1];
            ossClient.deleteObject(bucketName, objectKey);
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
    }

    public static String getContentType(String FilenameExtension) {
        if (FilenameExtension.equalsIgnoreCase(".bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equalsIgnoreCase(".gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
                FilenameExtension.equalsIgnoreCase(".jpg") ||
                FilenameExtension.equalsIgnoreCase(".png")) {
            return "image/jpg";
        }
        if (FilenameExtension.equalsIgnoreCase(".html")) {
            return "text/html";
        }
        if (FilenameExtension.equalsIgnoreCase(".tif")) {
            return "image/tif";
        }
        if (FilenameExtension.equalsIgnoreCase(".TIF")) {
            return "image/tif";
        }
        if (FilenameExtension.equalsIgnoreCase(".txt")) {
            return "text/plain";
        }
        if (FilenameExtension.equalsIgnoreCase(".vsd")) {
            return "application/vnd.visio";
        }
        if (FilenameExtension.equalsIgnoreCase(".pptx") ||
                FilenameExtension.equalsIgnoreCase(".ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (FilenameExtension.equalsIgnoreCase(".docx") ||
                FilenameExtension.equalsIgnoreCase(".doc")) {
            return "application/msword";
        }
        if (FilenameExtension.equalsIgnoreCase(".xml")) {
            return "text/xml";
        }
        return "image/jpg";
    }

}
